<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Storagemanage extends CI_Controller {

	var $limit;

	public function __construct()
	{
		parent::__construct();	

		$logged_in = $this->session->userdata('logged_in');
		if($logged_in)
		{
		   $session_data = $this->session->userdata('logged_in');
		   $data['username'] = $session_data['username'];		   
		}
		else
		{
	     //If no session, redirect to login page
	     redirect('login', 'refresh');
   		}

		// gán giá trị tiêu đề form
		$this->template->set('nav',$this->uri->segment(1));
		$this->template->set('title', 'Số lượng áo trong kho');
		$this->template->set('section', 'Số lượng áo trong kho');	

		// load model kho
		$this->load->model('Storage_model');	
		// load model history kho
		$this->load->model('Storage_product_history_model');
		// load products that in storage
		$this->load->model('Storage_product_model');
		// load product size
		$this->load->model('Product_sizes_model');
		// load product model
		$this->load->model('Product_model');

		// grant limit to 10
		$this->limit = 10;
	}


	public function index($p = false)
	{
		if(empty($p))
			$p = 1;

		$start = ($p - 1) * $this->limit;

		// all storage
		$return = $this->Product_model->loadAllV2($start, $this->limit);

		$data['products'] = $return['data'];
		$total_products = $return['count'];

		$data['total_products'] = $total_products;


		////////////////// pagination //////////////////////////////////////
		$this->load->library('pagination');				

		$config['base_url'] = site_url('storagemanage/index/');
		$config['total_rows'] = $total_products;
		$config['per_page'] = $this->limit;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		// first link
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		// next link
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';	
		// previous link
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		// end link
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		// current
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
		// digit link
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';

		$this->pagination->initialize($config); 

		$data['paging_products'] = $this->pagination->create_links();

		////////////////////////////////////////////////////////////////////

		// load all products
		$data['product_infos'] = array();
		foreach ($data['products'] as $product) 
		{
			$product_id = $product->id;
			$product_title = $product->title;
			$product_color = $product->color;

			$size_infos = $this->Product_sizes_model->getInfoSizeProduct($product_id);

		 	$xxxl = $xxl = $xl = $xs = $s = $m = $l = "";

		 	foreach ($size_infos as $size) 
		 	{
		 		$size_txt = $size->size;

		 		if($size_txt == 'xxxl')
		 		{
		 			$xxxl = $size->quantity;
		 		}
		 		else if($size_txt == 'xxl')
		 		{
		 			$xxl = $size->quantity;
		 		}
		 		else if($size_txt == 'xl')
		 		{
		 			$xl = $size->quantity;
		 		}
		 		else if($size_txt == 'xs')
		 		{
		 			$xs = $size->quantity;
		 		}
		 		else if($size_txt == 's')
		 		{
		 			$s = $size->quantity;	
		 		}
		 		else if($size_txt == 'm')
		 		{
		 			$m = $size->quantity;
		 		}
		 		else if($size_txt == 'l')
		 		{
		 			$l = $size->quantity;
		 		}
		 	}

		 	$data['product_infos'][] = array(
		 			'title'	=> $product_title,
		 			'color'	=> $product_color,
		 			'xxxl'  => $xxxl,
		 			'xxl'	=> $xxl,
		 			'xl'	=> $xl,
		 			'xs'	=> $xs,
		 			's'		=> $s,
		 			'm'		=> $m,
		 			'l'		=> $l,
		 		);
		}

		$this->template->load('admin/template','admin/storagemanage',$data);
	}

	public function addproduct()
	{
		$this->template->set('title', 'Tạo sản phẩm');
		$this->template->set('section', 'Tạo sản phẩm trong kho');	
		$this->template->load('admin/template','admin/addproduct');
	}

	public function addproduct_process()
	{
		$title = $this->input->post('title');
		$color = $this->input->post('color');
		$code = $this->input->post('code');

		// if(strstr($title, "'"))
		// {
		// 	$title = str_replace("'", "''", $title);
		// }

		// create slug
		$slug = $this->accents->removeaccents($title);
		$slug = url_title($slug);

		$data = array(
				'title'	=>	$title,
				'code'	=>	$code,
				'color'	=>	$color,
				'slug'	=>	$slug,
			);

		$this->Product_model->insert($data);

		$product_id = $this->db->insert_id();

		if($product_id > 0)
		{
			$xs = $this->input->post('xs');

			if(!empty($xs))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$xs,
						'size'			=>	'xs',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$s = $this->input->post('s');

			if(!empty($s))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$s,
						'size'			=>	's',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$m = $this->input->post('m');

			if(!empty($m))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$m,
						'size'			=>	'm',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$l = $this->input->post('l');

			if(!empty($l))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$l,
						'size'			=>	'l',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$xl = $this->input->post('xl');

			if(!empty($xl))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$xl,
						'size'			=>	'xl',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$xxl = $this->input->post('xxl');

			if(!empty($xxl))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$xxl,
						'size'			=>	'xxl',						
					);
				$this->Product_sizes_model->insert($data);
			}

			$xxxl = $this->input->post('xxxl');

			if(!empty($xxxl))
			{
				$data = array(
						'product_id'	=>	$product_id,
						'code'			=>	$xxxl,
						'size'			=>	'xxxl',						
					);
				$this->Product_sizes_model->insert($data);
			}
		}

		redirect('storagemanage/index');
	}
}	
